package com.wh.wisdomsite.xbox.server.thread;

import com.wh.wisdomsite.xbox.server.listener.RedisMsgPubSubListener;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * redis订阅者线程
 *
 * @version V1.0
 * @Package: com.wh.wisdomsite.xbox.server.thread
 * @author: 丁奕
 * @date: 2018-09-25 11:05
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 **/
public class SubThread extends Thread {
    //redis订阅的频道
    private final String channel = "::events/alarm2";

    //jedis连接池
    private final JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "111.85.150.22", 16379);

    //redis订阅者
    private final RedisMsgPubSubListener redisMsgPubSubListener = new RedisMsgPubSubListener();

    public SubThread() {
        super("SubThread");
    }

    @Override
    public void run() {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();   //取出一个连接
            jedis.subscribe(redisMsgPubSubListener, channel);    //通过subscribe 的api去订阅，入参是订阅者和频道名
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
